package com.zgroup.domain

import com.zgroup.common.CheckUtils
import com.zgroup.common.Pinyin4jUtils
import com.zgroup.domain.SysUser


class Estate {
    def estateService
    def userService
    final static int INFO_MAX_NUM_VIP = 8
    final static int INFO_MAX_NUM_FIRST = 4
    final static int INFO_MAX_NUM_SECOND = 2
    final static int INFO_MAX_NUM_THIRD = 1
    final static String ESTATE_TYPE_SECTOR = "10"
    final static String ESTATE_TYPE_CIRCLE = "20"

    final static String DEFAULT_COVER_PIC = "/images/ui/default_estate_pic.gif"

    final static String UPLOAD_FILE_PATH = "/upload/estate_picture/"     //发布信息图片上传路径
    final static String UPLOAD_THUMBNAIL_PATH = "/upload/estate_thumbnail/"     //发布信息缩略图上传路径

    String name         // 名称
    String pinyin       // 拼音首字母
    String type         // 类型（10：住宅/20：商用）
    String area         // 区域
    String sector       // 板块/商圈
    String address      // 地址

    String showFlag = '1'  // 显示标记位 1：显示 0：不显示
    String approveFlag = '0'  // 审批通过标记 0:审核中 1：通过  2:不通过

    Bid curMthVipBid     // 本月VIP竞标
    Bid vipBid
    Bid firstBid
    Bid secondBid
    Bid thirdBid

    User createUser
    SysUser approveUser

    Date dateCreated
    Date lastUpdated
    Date dateApproved

    Integer estPicNum = 0  //楼盘图片数
    Integer estAcceptPicNum = 0    //楼盘入库图片数
    Integer layoutPicNum = 0    //房型图片数
    Integer layoutAcceptPicNum = 0  //房型入库图片数
    Integer housePicNum = 0    //室内图片数

    // 追加字段
    String buildingType     //建筑类型
    String propertyType     //物业类型
    String parking          //停车位
    String buildingSqm     //建筑面积（单位：平方米）
    String volumeRatio     //容积率
    String greeningRatio   //绿化率
    String developer        //开发商
    String propertyCompany //物业公司
    String households       //总户数
    String managetmentFee  //管理费
    String completionDate  //竣工日期
    String surrounding      //周边配套
    String summary          //简介
    String supportingService //小区配套

    def beforeInsert = {
        pinyin = Pinyin4jUtils.getPinYinHeadChar(name)

    }
    def beforeUpdate = {
        pinyin = Pinyin4jUtils.getPinYinHeadChar(name)

    }
    String getTypeName() {
        //类型（10：住宅/20：商用）
        if (ESTATE_TYPE_SECTOR == type) {
            "住宅"
        } else if (ESTATE_TYPE_CIRCLE == type) {
            "商用"
        }
    }

    String getAreaName() {
        if (area != null) {
            Dictionary.findByTypeAndCode("SH_DISTRICT", area)?.name
        }
    }

    String getSectorName() {
        if (sector != null) {
            String dicType = "SECTOR"
            if (type == ESTATE_TYPE_CIRCLE) {
                dicType = "CIRCLE"
            }
            Dictionary.findByTypeAndCode(dicType, sector)?.name
        }
    }

    def getUserCount() {
        return userService.countByEstate(this)
    }
    def getRentInfoCount() {
        return estateService.countSupplyInfo(this,InfoRelease.INFO_TYPE_RENT)
    }
    def getSaleInfoCount() {
        return estateService.countSupplyInfo(this,InfoRelease.INFO_TYPE_SALE)
    }
    def getInfoCount() {
        return infoReleases?.size()
    }

    InfoRelease getRandomInfo() {
        if (infoReleases == null) {
            return null
        }
        int randomIndex = new Random().nextInt(infoReleases.size())
        return infoReleases.toArray()[randomIndex]
    }

    String getThumbCoverPic() {
        return DEFAULT_COVER_PIC
    }

    BigDecimal getBidPrice() {
        def price = firstBid?.money
        def setting = Setting.get(1)
        if (price == null) {
            price = setting.bidUpset
        }
        return price + setting.bidIncrements
    }

    BigDecimal getVipPrice() {
        def price = vipBid?.money
        def setting = Setting.get(1)
        if (price == null) {
            price = setting.vipUpset
        }
        return price + setting.vipIncrements
    }

    String getDisplayBidPrice() {
        def price = getBidPrice()
        if ("30" == firstBid?.payment) return price
        return price.toBigInteger()
    }

    String getDisplayVipPrice() {
        def price = getVipPrice()
        if ("30" == vipBid?.payment) return price
        return price.toBigInteger()
    }

    List getCurMthVipRandomInfoList(){
        return getRandomInfoList(curMthVipBid?.user,INFO_MAX_NUM_VIP)
    }
    List getFirstRandomInfoList(){
        return getRandomInfoList(firstBid?.user,INFO_MAX_NUM_FIRST)
    }
    List getSecondRandomInfoList(){
        return getRandomInfoList(secondBid?.user,INFO_MAX_NUM_SECOND)
    }
    List getThirdRandomInfoList(){
        return getRandomInfoList(thirdBid?.user,INFO_MAX_NUM_THIRD)
    }

    InfoRelease getCurMthVipRandomInfo(){
        def randomInfoList= getCurMthVipRandomInfoList()
        if (randomInfoList == null||randomInfoList.isEmpty()) {
          return null
        }
        return randomInfoList.get(0)
    }
    InfoRelease getFirstRandomInfo(){
        def randomInfoList= getFirstRandomInfoList()
        if (randomInfoList == null||randomInfoList.isEmpty()) {
          return null
        }
        return randomInfoList.get(0)
    }
    InfoRelease getSecondRandomInfo(){
        def randomInfoList= getSecondRandomInfoList()
        if (randomInfoList == null||randomInfoList.isEmpty()) {
          return null
        }
        return randomInfoList.get(0)
    }
    InfoRelease getThirdRandomInfo(){
        def randomInfoList= getThirdRandomInfoList()
        if (randomInfoList == null||randomInfoList.isEmpty()) {
          return null
        }
        return randomInfoList.get(0)
    }

    Integer getCurMthVipInfoCount(){
        return getInfoCount(curMthVipBid?.user)
    }
    Integer getFirstInfoCount(){
        return getInfoCount(firstBid?.user)
    }
    Integer getSecondInfoCount(){
        return getInfoCount(secondBid?.user)
    }
    Integer getThirdInfoCount(){
        return getInfoCount(thirdBid?.user)
    }

    List getRandomInfoList(pUser,pMaxSize){
        if(!CheckUtils.isEmpty(pUser)){
           return userService.randomEstateInfoList(pUser,this,pMaxSize)
        }
        return null;
    }

    Integer getInfoCount(pUser){
        if(!CheckUtils.isEmpty(pUser)){
           return userService.countEstateInfo(pUser,this)
        }
        return 0;
    }

    static transients = [
        "typeName", "areaName", "sectorName", "userCount", "infoCount", "randomInfo", "thumbCoverPic", "bidPrice", "vipPrice", "displayBidPrice", "displayVipPrice", "rentInfoCount","saleInfoCount",
        "curMthVipRandomInfoList","curMthVipRandomInfo","curMthVipInfoCount","firstRandomInfoList","firstRandomInfo","firstInfoCount","secondRandomInfoList","secondRandomInfo","secondInfoCount","thirdRandomInfoList","thirdRandomInfo","thirdInfoCount"
    ]

    static hasMany = [infoReleases: InfoRelease,estatePictures:EstatePicture]

    // VIP,冠军，亚军，季军
    //static belongsTo =[vipBid:Bid,firstBid:Bid,secondBid:Bid,thirdBid:Bid]

    static constraints = {
        name(blank: false, maxSize: 20)
        pinyin(blank: false, maxSize: 20)
        type(blank: false, maxSize: 6)
        area(blank: false, maxSize: 6)
        sector(blank: false, maxSize: 10)
        address(maxSize: 255)
        approveFlag(maxSize: 6)
        showFlag(maxSize: 6)
        approveUser(nullable: true)
        curMthVipBid(nullable: true)
        vipBid(nullable: true)
        firstBid(nullable: true)
        secondBid(nullable: true)
        thirdBid(nullable: true)
        dateApproved(nullable: true)

        buildingType(nullable: true, maxSize: 6)
        propertyType(nullable: true, maxSize: 6)
        parking(nullable: true, maxSize: 30)
        buildingSqm(nullable: true, maxSize: 30)
        volumeRatio(nullable: true, maxSize: 30)
        greeningRatio(nullable: true, maxSize: 30)
        developer(nullable: true, maxSize: 30)
        propertyCompany(nullable: true, maxSize: 30)
        households(nullable: true, maxSize: 30)
        managetmentFee(nullable: true, maxSize: 30)
        completionDate(nullable: true, maxSize: 30)
        surrounding(nullable: true)
        summary(nullable: true)
        supportingService(nullable: true)
        
    }

    static mapping = {
        dynamicInsert true
        dynamicUpdate true
        surrounding type: "text"
        summary type: "text"
        supportingService type: "text"
        sort "lastUpdated"
        order "desc"
    }

    String toString() {
        "id:" + id + ",name:" + name + ",area:" + area + ",sector:" + sector + ",firstBid:" + firstBid + ",secondBid:" + secondBid + ",thirdBid:" + thirdBid
    }
}
